<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>


<form:form commandName="model" cssClass="validate" id="ciForm"
	action="configuration/ci/save">
	<form:hidden path="id" />
	<input type="hidden" name="changeId" value="${changeId }" />
	<input type="hidden" value="${editStatus }" />

	<div id="confCiTab" class="box tabbedBox">
		<div class="header">
			<ul>
				<li class="current"><a href="#basicInfo">资产信息</a>
				</li>
				<li><a href="#relatedCi"
					data-beforeclick="relatedCi_beforeClick();">关联资产(<label
						id="ciRelatedCiCount" style="color:red"></label>)</a></li>
				<li><a href="#relatedDataLog">数据修改日志(<label
						id="ciDataLogCount" style="color:red"></label>)</a></li>
				<li><a href="#relatedIncident">事件履历(<label
						id="ciIncidentCount" style="color:red"></label>)</a>
				</li>
				<li><a href="#relatedChange">变更履历(<label id="ciChangeCount"
						style="color:red"></label>)</a>
				</li>
			</ul>
			<h2>&nbsp;</h2>
		</div>
		<div style="padding:0" class="content tabbed">
			<div id="basicInfo">
				<div class="box  no-border">
					<div class="header">
						<h3>&nbsp;&nbsp;基本信息</h3>
					</div>
					<table class=" form-table" style="table-layout: fixed;">
						<tbody>
							<tr>
								<td class="td1">名称：</td>
								<td class="td2"><form:input path="name" cssClass="required" />
								</td>
								<td class="td1">编号：</td>
								<td class="td2"><form:input path="code" cssClass="required" />
								</td>

							</tr>

							<tr>
								<td class="td1">类别：</td>
								<td class="td2"><c:choose>
										<c:when test="${empty model.confCategoryId }">
											<form:hidden path="confCategoryId" />
											<isperp:treeSelector id="confCategoryName"
												name="confCategoryName" displayValueCssClass="required"
												afterSelected="setCategoryProperty();"
												url="configuration/category/getTree"
												valueControlId="confCategoryId"
												displayValue="${model.confCategoryName}"></isperp:treeSelector>
										</c:when>
										<c:otherwise>
											<form:hidden path="confCategoryId" />
								${model.confCategoryName}
							</c:otherwise>

									</c:choose>
								</td>
								<td class="td1">状态：</td>
								<td class="td2"><form:select path="status"
										data-allow-empty="true" items="${ciStatus }" itemLabel="name"
										itemValue="name" cssClass="required"></form:select></td>
							</tr>



							<tr>
								<td class="td1">所属客户：</td>
								<td class="td2"><form:hidden path="cusCustomerId" /> <isperp:listSelector
										id="customerSelector" url="customer/customer/list"
										afterSelected="cusCustomerId_afterSelected(rows);"
										name="cusCustomerName"
										displayValue="${model.cusCustomerName }"
										valueControlId="cusCustomerId">
										<isperp:column name="id" title="id" hidden="true" width="10"></isperp:column>
										<isperp:column name="name" title="名称" width="150">
										</isperp:column>
										<isperp:column name="type" title="类型" width="80">
										</isperp:column>
										<isperp:column name="linkman" title="联系人" width="80">
										</isperp:column>
										<isperp:column name="linktel" title="联系电话" width="80">
										</isperp:column>
									</isperp:listSelector></td>

								<td class="td1">使用者：</td>
								<td class="td2"><form:hidden path="useStaffId" /> <isperp:listSelector
										id="useStaffSelector" reloadOnOpen="true"
										url="customer/user/list" name="useStaffName"
										displayValue="${model.useStaffName }"
										valueControlId="useStaffId"
										afterSelected="useStaffSelector_afterSelected(rows);">
										<isperp:column name="id" title="id" hidden="true"></isperp:column>
										<isperp:column name="name" title="名称" width="80">
										</isperp:column>
										<isperp:column name="secretLevel" title="密级" width="100">
										</isperp:column>
										<isperp:column name="department" title="部门" width="100">
										</isperp:column>
										<isperp:column name="linkaddress" title="房间号" width="80">
										</isperp:column>
										<isperp:column name="tel" title="电话" width="100">
										</isperp:column>
									</isperp:listSelector><a class="nui-txt-link" href="javascript:void(0)"
									onclick="edituseStaffUser()">修改</a><a class="nui-txt-link"
									href="javascript:void(0)"
									onclick="$('#useStaffSelector').val('');$('#useStaffId').val('')">清除</a>
								</td>

							</tr>
							<tr>
								<td class="td1">部门：</td>
								<td class="td2"><form:input path="departmentName" /><a
									href="javascript:void(0)"
									onclick="setDepartmentNameFromUseStaff()" class="nui-txt-link">根据责任人获取</a>
								</td>


								<td class="td1">位置：</td>
								<td class="td2"><form:input path="address" /><a
									href="javascript:void(0)" onclick="setAddressFromUseStaff()"
									class="nui-txt-link">根据责任人获取</a>
								</td>
							</tr>


							<tr>
								<td class="td1">备注：</td>
								<td class="td2" colspan="3"><form:textarea path="meno" />
								</td>
							</tr>
							<tr>
								<td class="td1">序列号：</td>
								<td class="td2"><form:input path="assetCode"
										cssClass="required" />
								</td>

								<td class="td1">制造商：</td>
								<td class="td2"><form:hidden path="manufacturerProviderId" />
									<isperp:listSelector id="manufacturerProviderId"
										url="provider/provider/list"
										displayValue="${model.manufacturerProviderName }"
										name="manufacturerProviderName"
										valueControlId="manufacturerProviderId">
										<isperp:column name="id" title="id" hidden="true"></isperp:column>
										<isperp:column name="code" title="编号" width="80">
										</isperp:column>
										<isperp:column name="name" title="名称" width="150">
										</isperp:column>
										<isperp:column name="linkman" title="联系人" width="80">
										</isperp:column>
										<isperp:column name="linktel" title="联系电话" width="80">
										</isperp:column>
										<isperp:column name="serviceTel" title="客服电话" width="80">
										</isperp:column>
										<isperp:column name="supportTel" title="技术支持电话" width="80">
										</isperp:column>
									</isperp:listSelector></td>
							</tr>

							<tr>
								<td class="td1">管理者：</td>
								<td class="td2"><form:hidden path="managementStaffId" /> <isperp:listSelector
										id="managementStaffIdSelector"
										url="customer/user/list?customerId=${model.cusCustomerId }"
										name="managementStaffName"
										displayValue="${model.managementStaffName }"
										valueControlId="managementStaffId">
										<isperp:column name="id" title="id" hidden="true"></isperp:column>
										<isperp:column name="name" title="名称" width="80">
										</isperp:column>
										<isperp:column name="type" title="类型" width="60">
										</isperp:column>
										<isperp:column name="sex" title="性别" width="60">
										</isperp:column>

										<isperp:column name="mobile" title="手机" width="100">
										</isperp:column>
										<isperp:column name="title" title="职务" width="100">
										</isperp:column>
									</isperp:listSelector></td>



								<td class="td1">集成商：</td>
								<td class="td2"><form:hidden path="integratorProviderId" />
									<isperp:listSelector id="integratorProviderIdSelector"
										url="provider/provider/list" name="integratorProviderName"
										displayValue="${model.integratorProviderName }"
										valueControlId="integratorProviderId">
										<isperp:column name="id" title="id" hidden="true"></isperp:column>
										<isperp:column name="code" title="编号" width="80">
										</isperp:column>
										<isperp:column name="name" title="名称" width="150">
										</isperp:column>
										<isperp:column name="linkman" title="联系人" width="80">
										</isperp:column>
										<isperp:column name="linktel" title="联系电话" width="80">
										</isperp:column>
										<isperp:column name="serviceTel" title="客服电话" width="80">
										</isperp:column>
										<isperp:column name="supportTel" title="技术支持电话" width="80">
										</isperp:column>
									</isperp:listSelector>
								</td>
							</tr>
							<tr>
								<td class="td1">维护者：</td>
								<td class="td2"><form:hidden path="supportStaffId" /> <isperp:listSelector
										id="supportStaffIdSelector"
										url="customer/user/list?customerId=${model.cusCustomerId }"
										name="supportStaffName"
										displayValue="${model.supportStaffName }"
										valueControlId="supportStaffId">
										<isperp:column name="id" title="id" hidden="true"></isperp:column>

										<isperp:column name="name" title="名称" width="80">
										</isperp:column>
										<isperp:column name="type" title="类型" width="60">
										</isperp:column>
										<isperp:column name="sex" title="性别" width="60">
										</isperp:column>

										<isperp:column name="mobile" title="手机" width="100">
										</isperp:column>
										<isperp:column name="title" title="职务" width="100">
										</isperp:column>
									</isperp:listSelector></td>
								<td class="td1">维保商：</td>
								<td class="td2"><form:hidden path="maintainerProviderId" />
									<isperp:listSelector id="maintainerProviderIdSelector"
										url="provider/provider/list" name="maintainerProviderName"
										displayValue="${model.maintainerProviderName }"
										valueControlId="maintainerProviderId">
										<isperp:column name="id" title="id" hidden="true"></isperp:column>
										<isperp:column name="code" title="编号" width="80">
										</isperp:column>
										<isperp:column name="name" title="名称" width="150">
										</isperp:column>
										<isperp:column name="linkman" title="联系人" width="80">
										</isperp:column>
										<isperp:column name="linktel" title="联系电话" width="80">
										</isperp:column>
										<isperp:column name="serviceTel" title="客服电话" width="80">
										</isperp:column>
										<isperp:column name="supportTel" title="技术支持电话" width="80">
										</isperp:column>
									</isperp:listSelector>
								</td>
							</tr>

							<tr>
								<td class="td1">维保合同：</td>
								<td class="td2"><isperp:uploader sourceId="${model.id }"
										id="ciContracts"></isperp:uploader>
								</td>
								<td class="td1">保修电话：</td>
								<td class="td2"><form:input path="repairTel" />
								</td>
							</tr>
							<tr>
								<td class="td1">采购日期：</td>
								<td class="td2"><form:input path="buyDate" type="date" />
								</td>
								<td class="td1">接收日期：</td>
								<td class="td2"><form:input path="receiveDate" type="date" />
								</td>
							</tr>
							<tr>
								<td class="td1">安装日期：</td>
								<td class="td2"><form:input path="installationDate"
										type="date" />
								</td>
								<td class="td1">在用日期：</td>
								<td class="td2"><form:input path="availableDate"
										type="date" /></td>
							</tr>
							<tr>
								<td class="td1">报废日期：</td>
								<td class="td2"><form:input path="expiredDate" type="date" />
								</td>
								<td class="td1">维保日期：</td>
								<td class="td2"><form:input path="maintainerStartDate"
										type="date" style="width:70px" /> 到 <form:input
										path="maintainerEndDate" type="date" style="width:70px" />
								</td>
							</tr>
						</tbody>
					</table>
				</div>

				<div id="ciPropertyDetail" class="box  no-border">
					<div class="header">
						<h3>&nbsp;&nbsp;扩展信息（不同类别的资产有不同的属性）</h3>
					</div>
					<table id="ciPropertyTable" class="form-table">
					</table>
				</div>
				<div class="box no-border">
					<div class="header">
						<h3>&nbsp;&nbsp;其它信息</h3>
					</div>
					<table class=" form-table">
						<tbody>
							<tr>
								<td class="td1">创建者：</td>
								<td class="td2"><form:input path="creator" readonly="true" />
								</td>
								<td class="td1">创建时间：</td>
								<td class="td2"><form:input path="createDate"
										readonly="true" /></td>
							</tr>

							<tr>
								<td class="td1">最后变更者：</td>
								<td class="td2"><form:input path="finalChanger"
										readonly="true" />
								</td>
								<td class="td1">变更时间：</td>
								<td class="td2"><form:input path="finalChangedDate"
										readonly="true" />
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
			<div id="relatedCi">
				<div class="box no-border">

					<isperp:grid url="configuration/ciRelation/listDetail"
						urlExtScript="function(){return '?fromId=${model.id }'}"
						gridId="ciRelationGrid" showPager="true" height="auto"
						showSearchbar="false" rowNum="1000"
						cusDeleteUrl="configuration/ciRelation/delete?changeId=${changeId }"
						grouping="true" groupField="relation_name"
						afterActionsSuccess="location.reload()"
						loadComplete="$('#ciRelatedCiCount').text( ciRelationGrid.jqGrid.getGridParam('records'));">

						<jsp:attribute name="toolbar">
						 
						 <span id="addRelationBtns" style=""></span>
						 <isperp:button id="ciRelationTypeCreate" icon="icon-plus"
								text="重建资产关联关系" onclick="rebuildAllRelation();"></isperp:button>
						 
						</jsp:attribute>

						<jsp:body>
							<isperp:column name="id" title="id" hidden="true"></isperp:column>
							<isperp:column name="relation_name" title="关联类型" hidden="true">
							</isperp:column>
							<isperp:column name="from_id" title="from_id" hidden="true">
							</isperp:column>
							<isperp:column name="to_id" title="to_id" hidden="true">
							</isperp:column>
							<isperp:column name="from_manage_code" title="from_manage_code"
								hidden="true">
							</isperp:column>
							<isperp:column name="to_manage_code" title="to_manage_code"
								hidden="true">
							</isperp:column>
							<isperp:column name="from_name" title="主关联设备" width="100"
								isFormatterFun="true"
								formatter="return formatterCi(cellvalue,rowObject,rowObject.from_id,rowObject.from_manage_code);">
							</isperp:column>
							<isperp:column name="from_secret_level" title="密级" width="40"></isperp:column>
							
							<isperp:column name="to_name" title="被关联设备" width="100"
								isFormatterFun="true"
								formatter="return formatterCi(cellvalue,rowObject,rowObject.to_id,rowObject.to_manage_code);">
							</isperp:column>
						 
							<isperp:column name="to_secret_level" title="密级" width="40"></isperp:column>
						
						</jsp:body>
					</isperp:grid>
				</div>
			</div>
			<div id="relatedDataLog">
				<div id="relatedDataLogInfo"></div>
				<script type="text/javascript">
				$.get("configuration/ci/ciDataChangedLog?id=${model.id}",function(data){
					$("#relatedDataLogInfo").html(data);
					
					$('#ciDataLogCount').text($(data).find("#ciDataLogCount").val());
				});
			</script>
			</div>
			<div id="relatedIncident">
				<div id="relatedIncidentInfo"></div>
				<script type="text/javascript">
				$.get("configuration/ci/ciFailureResume?id=${model.id}",function(data){
					$("#relatedIncidentInfo").html(data);
					
					$('#ciIncidentCount').text($(data).find("#ciIncidentCount").val());
				});
			</script>
			</div>
			<div id="relatedChange">
				<div id="relatedChangeInfo"></div>
				<script type="text/javascript">
				$.get("configuration/ci/ciChangeResume?id=${model.id}",function(data){
					$("#relatedChangeInfo").html(data);
					
					$('#ciChangeCount').text($(data).find("#ciChangeCount").val());
				});
			</script>
			</div>
		</div>

	</div>

</form:form>

<input type="hidden" id="ciSelectorIds">
<isperp:listSelector id="ciSelector" displayValueCssClass="hidden"
	selectorCssClass="hidden"
	url="configuration/ciRelation/listRelation?fromId=${model.id}"
	afterSelected="ciSelector_afterSelected(rows);" multiselect="true">
	<isperp:column name="id" title="id" hidden="true"></isperp:column>
	<isperp:column name="code" title="编号" width="80">
	</isperp:column>
	<isperp:column name="manageCode" title="管理编号" width="80">
	</isperp:column>
	<isperp:column name="name" title="名称" width="100">
	</isperp:column>
	<isperp:column name="secretLevel" title="密级" width="60">
	</isperp:column>
	<isperp:column name="status" title="状态" width="60">
	</isperp:column>
	<isperp:column name="confCategoryName" title="类别" width="80">
	</isperp:column>
	<isperp:column name="departmentName" title="部门" width="100">
	</isperp:column>
	<isperp:column name="useStaffName" title="责任人" width="60">
	</isperp:column>
	<isperp:column name="address" title="房间号" width="60">
	</isperp:column>
	<isperp:column name="meno" title="房间号" width="60">
	</isperp:column>
</isperp:listSelector>

<isperp:buttonList>
	<button class="submit nui-mainbtn"
		onclick="submitCiForm();return false;">
		<span class="icon icon-save"></span>保存
	</button>
</isperp:buttonList>

<input type="hidden" id="relationTypeId">
<script type="text/javascript">
	function rebuildAllRelation() {

		isperp.confirmAlert("您确定要重新生成此资产项的关联关系吗？<br><span style='color:red'>警告：此功能将会现有的配置关联，请谨慎使用！</span>", {
			yes : function() {
				isperp.showLoading();

				$.post("configuration/ciRelation/rebuildAll?changeId=${changeId }id="+$("#id").val(), function(data) {
					if (data.success) {
						ciRelationGrid.reload();
						isperp.alert("重建成功！");
					} else {
						isperp.errorAlert(data.data);
					}
					isperp.hideLoading();

				});
			}
		});

	}
	
	function relatedCi_beforeClick(){
		var eStatus=$("#editStatus").val();
		
		if(eStatus=="1"){
			isperp.alert("请先保存资产信息！");
			return false;
		}
		return true;
		
		
	}
</script>
<script type="text/javascript">
	$(document).ready(function() {
		setCategoryProperty();
		useStaffSelectorSelector.setParam("customerId=${model.cusCustomerId}");
	});
	
	
	function edituseStaffUser(){
	
		var userId=$("#useStaffId").val();
		if(userId==undefined||userId==""){
			isperp.alert("请先选择负责人");
			return false;
		}
		
		isperp.openDialog({contentUrl:"customer/user/edit?id="+userId,windowSize:"normal"});
		
	}

	function formatterCi(cellvalue, rowObject, id,manageCode) {
		var mId = "${model.id}";
		if (id == mId) {
			return cellvalue+ '（'+manageCode+'）';
		} else {
			return '<a class="nui-txt-link" href="javascript:void(0)" onclick="isperp.openDialog({contentUrl:\'configuration/ci/edit?id=' + id + '\'})">' + cellvalue + '（'+manageCode+'）</a>';
		}
	}

	function ciRelationGridEditCi(id) {
		var row = ciRelationGrid.getRowData(id);
		isperp.openDialog({
			contentUrl : "configuration/ci/edit?id=" + row.to_id
		});

	}

	function selectRelation(relationTypeId) {
		var propertyList = getPropertyControlValues();

		isperp.submitForm({
			formId : "ciForm",
			data : {
				"propertyList" : propertyList
			},
			success : function() {

				var $cateId = $("#confCategoryId");
				$cateId.attr("disabled", "disabled");
				$("#relationTypeId").val(relationTypeId);
				ciSelectorSelector.setParam("relationTypeId=" + relationTypeId + "&customerId=" + $("#cusCustomerId").val());

				$('#ciSelector').click();
			}
		});
		return false;

	}

	function cusCustomerId_afterSelected(rows) {
		if (rows.length < 1) {
			isperp.alert("没有数据被选择！")
			return false;
		}

		useStaffSelectorSelector.setParam("customerId=" + rows[0].id);
	}
	
	
	function useStaffSelector_afterSelected(rows) {
		if (rows.length < 1) {
			return false;
		}
		var departmentName = rows[0].department, address = rows[0].linkaddress, $departmentName = $("#departmentName"), $address = $("#address");
		
		if($departmentName.val()==""){
			$departmentName.val(departmentName);
		}else if ($departmentName.val() != departmentName) {
			isperp.confirmAlert("资产部门【" + $departmentName.val() + "】与责任人部门【" + departmentName + "】不一致，是否更新为责任人部门？", {
				yes : function() {
					$departmentName.val(departmentName);
				}
			});
		}
		if($address.val()==""){
			$address.val(address);
		}else if ($address.val() != address) {
			isperp.confirmAlert("资产房间号【" + $address.val() + "】与责任人房间号【" + address + "】不一致，是否更新为责任人房间号？", {
				yes : function() {
					$address.val(address);
				}
			});

		}
	}
	
	function setDepartmentNameFromUseStaff(){
		var userId=$("#useStaffId").val();
		if(userId==""){
			isperp.alert("请选择责任人！");
			return;
		}
		$.get("customer/user/getUser?id="+userId,function(data){
			$("#departmentName").val(data.department);
		});
	}
	
	function setAddressFromUseStaff(){
		var userId=$("#useStaffId").val();
		if(userId==""){
			isperp.alert("请选择责任人！");
			return;
		}
		$.get("customer/user/getUser?id="+userId,function(data){
			$("#address").val(data.linkaddress);
		});
	}
	

	function ciSelector_afterSelected(rows) {
		if (rows.length < 1) {
			isperp.alert("没有数据被选择！")
			return;
		}
		var ids = rows[0].id;
		for ( var i = 1; i < rows.length; i++) {
			var item = rows[i];
			ids = ids + "," + item.id;
		}
		
		$.post("configuration/ciRelation/batchSave", {
			relationTypeId : $("#relationTypeId").val(),
			fromId : '${model.id}',
			toIds : ids
		}, function(data) {
			location.reload()
		});

	}

	//提交配置项表达
	function submitCiForm() {

		var propertyList = getPropertyControlValues();

		isperp.submitForm({
			formId : "ciForm",
			data : {
				"propertyList" : propertyList
			},
			success : function() {
				isperp.alert("资产配置保存成功！");
				isperp.closeDialog();

				isperp.reloadParentControl();
			}
		});
		return false;

	}

	//设置分类的自定义属性并设置ci中所关联的值或者默认值
	function setCategoryProperty() {
		var confCategoryIdVal = $("#confCategoryId").val();
		if (confCategoryIdVal == undefined || confCategoryIdVal == "") {
			$("#ciPropertyTable").html("");
			return false;
		}
		$("#ciPropertyTable").html("载入自定义属性中...");

		$.getJSON("configuration/category/property/listByCategory?categoryId=" + confCategoryIdVal, function(data) {

			$("#ciPropertyTable").empty();
			var col = 2;
			var rStr = "";
			$(data).each(function(index, item) {
				col--;
				rStr += "<td class='td1'>" + item.name + "：</td><td class='td2'>" + createControl(item) + "</td>"
				if (index % 2 != 0) {
					var row = "<tr>" + rStr + "</tr>"
					rStr = "";
					col = 2;
					$("#ciPropertyTable").append(row);
				}
			});

			if (rStr != "") {
				$("#ciPropertyTable").append("<tr>" + rStr + "</tr>");
				rStr = "";
			}

			setCiPropertyValue();
			isperp.initElement("#ciPropertyTable");

		});

		setRelation(confCategoryIdVal);

	}

	function setRelation(val) {
		$.get("configuration/ciRelationType/listRelationTypes?fromCategoryId=" + val, function(data) {

			var html = "";
			$(data).each(function(index, item) {
				html += "<button  type='button' class='nui-mainbtn' onclick='selectRelation(\"" + item.id + "\")'><span class='icon icon-plus'></span>" + item.name + "</button>";

			});

			$("#addRelationBtns").html(html);

		});
	}

	//设置自定义ci的属性
	function setCiPropertyValue() {	
		
		$.ajax({
			type : "POST",
			url : "configuration/ci/listProperty",
			data : {
				id : $("#id").val()
			},
			async : false,
			dataType : "json",
			success : function(data) {
				$(data).each(function(index, item) {
					
					$("#c_" + item.confCategoryPropertyId).val(item.value);

				});
			}
		});
	}

	//获取自定义属性的值
	function getPropertyControlValues() {
		var $input = $("#ciPropertyTable input");
		var $select = $("#ciPropertyTable select");

		var count = 0;
		var result = "[";
		$($input).each(function(index, item) {
			var $el = $(item);

			if ($el.attr("id") && $el.val()) {

				if (count == 0) {
					result += "{'confCategoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				} else {
					result += ",{'confCategoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				}
				count++;
			}
		});
		$($select).each(function(index, item) {
			var $el = $(item);
			if ($el.attr("id") && $el.val()) {
				if (count == 0) {
					result += "{'confCategoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				} else {
					result += ",{'confCategoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				}
				count++;
			}
		});

		return result + "]";
	}

	//构建input元素
	function createInputElement(item, cssCls) {
		var controlStr = "<input type='" + item.data_type + "' id='c_" + item.id + "' data-id='" + item.id + "' class='" + cssCls;

		if (item.notnull) {
			controlStr += " required";
		}

		controlStr += "' />"
		return controlStr;
	}

	//构建自定义属性输入控件
	function createControl(item) {
		var controlStr = "";
		var cssCls = "";
		switch (item.data_type) {
		case "string":
			controlStr = createInputElement(item, cssCls);
			break;
		case "number":
			cssCls = "number";
			item.data_type = "text";
			controlStr = createInputElement(item, cssCls);
			break;
		case "date":
		case "datetime":
			controlStr = createInputElement(item, cssCls);
			break;
		case "textarea":
			controlStr = "<textarea  id='c_" + item.id + "' data-id='" + item.id + "' class='" + cssCls;

			if (item.notnull) {
				controlStr += " required";
			}

			controlStr += "'></textarea>";
			break;

		case "select":
			controlStr = "<select  id='c_" + item.id + "'   data-id='" + item.id + "' ";
			if (item.notnull) {
				controlStr += " class='required'>";
			} else {
				controlStr += " >";
			}

			controlStr+="<option value=''></option>";
			
			if (item.items) {

				var itemArr = item.items.split("|");

				$(itemArr).each(function(i, it) {
					controlStr += "<option value='"+it+"'>" + it + "</option>";
				});
			}

			controlStr += "</select>";
			break;

		}

		return controlStr;

	}
</script>
